﻿namespace Sage.WindowsHotKey.Exceptions
{
    /// <summary>
    /// Keys枚举的扩展方法类，提供按键转换和格式化功能
    /// </summary>
    public static class KeysExtensions
    {
        /// <summary>
        /// 扩展方法，将Keys转换为可读的按键组合字符串
        /// </summary>
        /// <param name="keyData">按键组合</param>
        /// <param name="continueText">当只有修饰键时显示的提示文本，为null时只显示加号</param>
        /// <returns>格式化的按键字符串，如 "Ctrl + Alt + A"</returns>
        public static string ToKeyString(this Keys keyData, string? continueText = null)
        {
            var parts = new List<string>();

            var modifiers = keyData & Keys.Modifiers;
            var mainKey = keyData & Keys.KeyCode;

            if ((modifiers & Keys.Control) == Keys.Control)
            {
                parts.Add("Ctrl");
            }
            if ((modifiers & Keys.Alt) == Keys.Alt)
            {
                parts.Add("Alt");
            }
            if ((modifiers & Keys.Shift) == Keys.Shift)
            {
                parts.Add("Shift");
            }

            if (mainKey != Keys.None &&
                mainKey != Keys.ControlKey &&
                mainKey != Keys.ShiftKey &&
                mainKey != Keys.Menu)
            {
                string mainKeyStr = GetMainKeyString(mainKey);
                parts.Add(mainKeyStr);
            }

            string result = string.Join(" + ", parts);

            if (modifiers != Keys.None &&
                (mainKey == Keys.None || mainKey == Keys.ControlKey ||
                 mainKey == Keys.ShiftKey || mainKey == Keys.Menu))
            {
                result += " +";
                if (!string.IsNullOrWhiteSpace(continueText))
                {
                    result += " " + continueText;
                }
            }

            return result;
        }

        /// <summary>
        /// 将按键字符串转换为Keys值的扩展方法
        /// </summary>
        /// <param name="keyString">按键字符串，可以是单键或组合键</param>
        /// <param name="separator">组合键的分隔符，默认为 "+"</param>
        /// <returns>解析后的Keys值</returns>
        public static Keys FromKeyString(this string keyString, string separator = "+")
        {
            return KeysStringParser.ParseKeyString(keyString, separator);
        }

        /// <summary>
        /// 获取主按键的显示字符串，将特殊按键转换为易读的格式
        /// </summary>
        /// <param name="mainKey">要转换的主按键</param>
        /// <returns>按键的显示字符串</returns>
        private static string GetMainKeyString(Keys mainKey) => mainKey switch
        {
            Keys.D0 => "0",
            Keys.D1 => "1",
            Keys.D2 => "2",
            Keys.D3 => "3",
            Keys.D4 => "4",
            Keys.D5 => "5",
            Keys.D6 => "6",
            Keys.D7 => "7",
            Keys.D8 => "8",
            Keys.D9 => "9",
            Keys.OemMinus => "-",
            Keys.Oemplus => "+",
            Keys.OemOpenBrackets => "[",
            Keys.OemCloseBrackets => "]",
            Keys.OemPeriod => ".",
            Keys.Oemcomma => ",",
            Keys.OemSemicolon => ";",
            Keys.OemQuotes => "'",
            Keys.Oemtilde => "`",
            Keys.OemBackslash => "\\",
            Keys.Space => "Space",
            _ => mainKey.ToString()
        };
    }

}
